function f = Objfun(C, mu, S2)

%fprintf('Cmax: %d, mumax: %d, delta: %d, 2S: %d\n', max(C),max(mu),abs(C(end)-mu(end)),2*sqrt(S2(end)));
% Cmax = max(C)
% mumax = max(mu)
% Cend = C(end)
% delta = (C(end)-mu(end))
% Send = 2*sqrt(S2(end))
if abs(C(end)-mu(end)) > 2.5*sqrt(S2(end));
    f = 1000;
elseif max(C) < 0.7*max(mu);
    f = 1000;
% elseif max(C) > 1.5*max(mu);
%     f = 1000;
else
    num = length(C);
    detaC = C-mu;
    f = num*0.5*log(2*pi)+ 0.5*sum(log(S2))+0.5*sum(detaC.*detaC./S2);
    f = f/10000;
end